
import { Component, OnInit, Input, Injectable, EventEmitter } from '@angular/core';
import { Location } from "@angular/common";
import { HttpClient } from '@angular/common/http';
import { HttpService } from '../../core/service/http.service';
import { SignalRService } from '../../core/service/singnalr-service';

import { Router, ActivatedRoute } from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, CustomNavigation, ElementShowType, CnBooleanPipe, KeyValueOutput,
  SonPagerSearchInput, LoadRemoteDataType, SortExpression, SortType
} from "../..";
import { NzMessageService, NzModalService, UploadFile } from 'ng-zorro-antd';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl,
  ValidatorFn,
  AbstractControl
} from "@angular/forms";
import { Observable, Observer, BehaviorSubject } from 'rxjs/';
import { map, filter, debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { isNumber, isBoolean } from 'util';
import { environment } from "environments/environment";

import { ClassGetter } from '@angular/compiler/src/output/output_ast';
@Component({
  selector: 'app-studentchat',
  templateUrl: './studentchat.component.html',
  styleUrls: ['./studentchat.component.scss']
})
export class StudentchatComponent implements OnInit {

  isCollapsed = false;
  triggerTemplate = null;
  inform; // 发送的消息
  websocket = null;
  sent;
  listdata = [];
  receiveid; // 接收学生id
  groupId; // 选择组的ID
  userid; //
  jsstudent = false;
  sendstudent = false;
  messagestudent = [];
  messagestudentas = [];
  chatlist = [];
  liststudent;
  searchs;
  name;
  userName;
  className = '';
  talkName = '';
  /**
   * 获取选择对象的聊天记录。
   */
  get userMsg() {
    let reslut = [];
    if (this.groupId && this.receiveid && this.signalrService.UserChatContext) {
      this.signalrService.UserChatContext.forEach(e => {
        if (e.id === this.groupId) {
          e.chatUsers.forEach(usr => {
            if (usr.id === this.receiveid && usr.chatInfos) {
              usr.chatInfos.forEach(msg => {
                msg.isRead = true;
              });
              reslut = usr.chatInfos;
            }
          });
        }
      });
    }
    const div = document.getElementsByClassName('scrollTop');
    div[0].scrollTop = div[0].scrollHeight + 1000;
    return reslut;
  }


  constructor(
    private message: NzMessageService,
    public http: HttpService,
    private signalrService: SignalRService,
  ) {
    this.userid = sessionStorage.getItem('userid'); // 用户id
    this.userName = sessionStorage.getItem('userName'); // 用户id
    const data = "";
    this.searchlist('');
    // this.signalrService.chatrole.subscribe(content => {

    //   // const div = document.getElementsByClassName('scrollTop');
    //   // this.messagestudent.push(e);
    //   // div[0].scrollTop = div[0].scrollHeight + 1000;
    // });
  }

  ngOnInit() {
    this.name = this.userName;
  }




  /**
   * 发送聊天信息
   */
  chatsent() {
    if (!this.receiveid) {
      this.message.info('请选择要发送的人员！');
      return false;
    }
    if (this.inform === "" || this.inform === undefined) {
      this.message.info('发送消息不能为空！');
    } else {
      this.signalrService.Chat(this.receiveid, this.inform.toString());
      this.signalrService.AddMessage(this.groupId, this.receiveid, this.inform, '我');
      this.inform = "";
    }
  }

  /**
   * 选择聊天对象
   * @param receiveid 
   * @param name 
   * @param classname 
   * @param groupid 
   */
  studentchat(receiveid, name, classname, groupid) {
    this.receiveid = receiveid;
    this.talkName = name;
    this.className = classname;
    this.groupId = groupid;
  }

  /**
   * 获取学生列表
   * @param data 
   */
  async searchlist(data) {
    if (data === undefined) {
      data = "";
    }
    this.listdata = await this.http.get<any>("StudentInfo/GetClassInfoToStudentById?searchName=" + data);
  }
}
